package top.stacktrace.BookInfo.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import top.stacktrace.BookInfo.domain.Book;

import java.util.List;

@Component
@Mapper
public interface BookDao {
    @Insert("insert into books (`title`,`desc`,`price`,`reserve`) values(#{title},#{desc},#{price},#{reserve})")
    int saveBook(Book b);

    // 增加仓库值
    @Update("update books set reserve=reserve+#{reserve} where id=#{id}")
    int inCreaseReserve(long id,int reserve);

    // 减少仓库值(判断不能扣负)
    @Update("update books set reserve=reserve-#{reserve} where id=#{id} and reserve>=#{reserve}")
    int deCreaseReserve(long id,int reserve);

    // 设置仓库值
    @Update("update books set reserve=#{reserve} where id=#{id}")
    int setReserve(long id,int reserve);

    @Select("select * from books")
    List<Book> getBooks();

    @Select("select * from books where id=#{id}")
    Book getBookById(long id);

    @Insert("insert into xalog (xid,service) values(#{xid},'bookinfo')")
    int saveXA(String xid);

    @Select("select count(1) from xalog where xid=#{xid} and service='bookinfo'")
    int countXA(String xid);

    @Delete("delete from xalog where xid=#{xid} and service='bookinfo'")
    int delXA(String xid);
}
